from math import radians, cos, sin, asin, sqrt

def dftolist(df):  #dataframe转列表
    dflist = [[] for k in range(len(df))]
    mmsi = df['mmsi'].reset_index(drop=True)
    longitude = df['longitude'].reset_index(drop=True)
    latitude = df['latitude'].reset_index(drop=True)
    cog = df['cog'].reset_index(drop=True)
    sog = df['sog'].reset_index(drop=True)
    # aistime = df['aistime'].reset_index(drop=True)
    Unixtime = df['Unixtime'].reset_index(drop=True)
    traj_segment = df['traj_segment'].reset_index(drop=True)
    for m in range(len(df)):
        dflist[m].append(mmsi[m])
        dflist[m].append(longitude[m])
        dflist[m].append(latitude[m])
        dflist[m].append(cog[m])
        dflist[m].append(sog[m])
        # dflist[m].append(aistime[m])
        dflist[m].append(Unixtime[m])
        dflist[m].append(traj_segment[m])
    return dflist

def getrdistance(point_a, point_b): # 输入两个点的信息
    lon1=point_a[1]
    lat1=point_a[2]
    lon2=point_b[1]
    lat2=point_b[2]
    """
    Calculate the great circle distance between two points
    on the earth (specified in decimal degrees)
    """
    # 将十进制度数转化为弧度
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

    # haversine公式
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))
    r = 6371 # 地球平均半径，单位为公里
    return round(c * r,3)

